package com.example.mobile2121606109ycg0516homework1;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private EditText edtTxtMainUserName;
    private EditText edtTxtMainPassword;
    private CheckBox chkBoxMainPassword;
    private CheckBox chkBoxMainAutoLogin;
    private Button btnMainLogin;
    private Button btnMainRegister;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        sp = getSharedPreferences("login",MODE_PRIVATE);

        chkBoxMainPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("chkPSW", b);
                editor.commit();
            }

        });

        chkBoxMainAutoLogin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("autoLogin",b);
                editor.commit();
                if (b){
                    chkBoxMainPassword.setChecked(true);
                    chkBoxMainPassword.setEnabled(false);
                }else {
                    chkBoxMainPassword.setEnabled(true);
                }
            }
        });

        btnMainLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userName = edtTxtMainUserName.getText().toString().trim();
                String password = edtTxtMainPassword.getText().toString().trim();

                if (userName.equals("admin") && password.equals("admin")){
                    if (chkBoxMainPassword.isChecked()){
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("userName",userName);
                        editor.putString("password", password);
                        editor.commit();
                    }
                    Intent intent = new Intent(MainActivity.this,LoginActivity.class);
                    intent.putExtra("userName1",userName);
                    startActivity(intent);
                }else {
                    new AlertDialog.Builder(MainActivity.this)
                            .setMessage("账号或密码错误")
                            .setPositiveButton("确定",null)
                            .create()
                            .show();
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        boolean chkPSW = sp.getBoolean("chkPSW",false);
        boolean autoLogin = sp.getBoolean("autoLogin",false);
        chkBoxMainPassword.setChecked(chkPSW);
        chkBoxMainAutoLogin.setChecked(autoLogin);
        if (chkPSW){
            edtTxtMainUserName.setText(sp.getString("userName",null));
            edtTxtMainPassword.setText(sp.getString("password",null));
        }

        if (autoLogin){
            Intent intent = new Intent(MainActivity.this,LoginActivity.class);
            intent.putExtra("userName1","admin");
            startActivity(intent);
        }
    }

    private void initView() {
        edtTxtMainUserName = (EditText) findViewById(R.id.edtTxtMainUserName);
        edtTxtMainPassword = (EditText) findViewById(R.id.edtTxtMainPassword);
        chkBoxMainPassword = (CheckBox) findViewById(R.id.chkBoxMainPassword);
        chkBoxMainAutoLogin = (CheckBox) findViewById(R.id.chkBoxMainAutoLogin);
        btnMainLogin = (Button) findViewById(R.id.btnMainLogin);
        btnMainRegister = (Button) findViewById(R.id.btnMainRegister);
    }
}